/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.cbr.similarity;

import java.util.List;
import jcolibri.exception.NoApplicableSimilarityFunctionException;
import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;
import my.cbr.database.model.AquacultureProdUnit;

/**
 *
 * @author Geir Ole
 */
public class ProdUnitAttributeSimilarity implements LocalSimilarityFunction {
    
    public double compute(Object caseObject, Object queryObject) throws NoApplicableSimilarityFunctionException {
        if(!this.isApplicable(caseObject, queryObject))
            return 0.0;
        
        
        List<AquacultureProdUnit> caseO, queryO;
        try {
            caseO = (List<AquacultureProdUnit>) caseObject;
            queryO = (List<AquacultureProdUnit>) queryObject;
        } catch (Exception e) {
            return 0.0;
        }
        
        double v1Individ = 0, v1Starvation = 0;
        double v2Individ = 0, v2Starvation = 0;
        
        for(AquacultureProdUnit apu : caseO)
        {
            Double individ = apu.getIndividCount();
            if(individ!=null)
                v1Individ += apu.getIndividCount();
        }
        for(AquacultureProdUnit apu : queryO)
        {
            Double individ = apu.getIndividCount();
            if(individ!=null)
                v2Individ += apu.getIndividCount();
        }

        for(AquacultureProdUnit apu : caseO)
        {
            Double starvation = apu.getIndividCount();
            if(starvation!=null)
                v1Starvation += apu.getStarvationDays();
        }
        for(AquacultureProdUnit apu : queryO)
        {
            Double starvation = apu.getIndividCount();
            if(starvation!=null)
                v2Starvation += apu.getStarvationDays();
        }

        double starvation = 1 - (Math.abs(v1Starvation-v2Starvation)/10);
        double individCount = 1 - (Math.abs(v1Individ-v2Individ)/300000);
        double returnValue = (starvation+individCount)/2;
        
        return returnValue;
    }

    public boolean isApplicable(Object caseObject, Object queryObject) {
        return (caseObject instanceof List)&&(queryObject instanceof List);
    }
    
}
